package com.ubxtech.web.handler.auth.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.ubxtech.web.handler.auth.LoginAppSecretService;
import com.ubxtech.core.dto.LoginAppSecretConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Victor.Xiao
 * @since 2022-03-29 11:47
 **/
@Slf4j
public class LoginAppSecretServiceImpl implements LoginAppSecretService {

    /**
     * 是否校验签名
     */
    private Integer checkFlag;
    /**
     * 授权的appId
     */
    private Map<String, LoginAppSecretConfig> appSecretsMap;

    public LoginAppSecretServiceImpl(String appSecretsStr , Integer checkFlag){
        this.checkFlag = checkFlag;
        if (StrUtil.isNotBlank(appSecretsStr)) {
            log.info("初始化配置{}",appSecretsStr);
            List<LoginAppSecretConfig> appSecretArr = JSONArray.parseArray(appSecretsStr, LoginAppSecretConfig.class);
            appSecretsMap = appSecretArr.stream().collect(Collectors.toMap(LoginAppSecretConfig::getAppId, a -> a,(k1, k2)->k1));
        }
    }

    @Override
    public Integer getCheckFlag() {
        return checkFlag;
    }

    @Override
    public Map<String, LoginAppSecretConfig> getAppSecretsConfig() {
        return appSecretsMap;
    }

}
